declare namespace g = "ddi:group:3_1";
declare namespace dc="ddi:dcelements:3_1";
declare namespace d="ddi:datacollection:3_1"; 
declare namespace dc2="http://purl.org/dc/elements/1.1/"; 
declare namespace s="ddi:studyunit:3_1"; 
declare namespace c="ddi:conceptualcomponent:3_1";
declare namespace r="ddi:reusable:3_1"; 
declare namespace a="ddi:archive:3_1"; 
declare namespace ddi="ddi:instance:3_1"; 
declare namespace l="ddi:logicalproduct:3_1";

declare namespace fo="http://www.w3.org/1999/XSL/Format";
declare namespace xslfo="http://exist-db.org/xquery/xslfo";

import module namespace request="http://exist-db.org/xquery/request";

import module namespace transform="http://exist-db.org/xquery/transform";

let $lang := request:get-parameter("lang", "sv")

let $id := request:get-parameter("id", ())

let $root := 
		collection('/db/ddi/data/ddi3/internal')/ddi:DDIInstance[s:StudyUnit/@id=$id]

let $params := <parameters>
				 <param name="lang"  value="{$lang}"/>
				 <param name="include-js"  value="true"/>
			</parameters>

let $xhtml := transform:transform($root, xs:anyURI("xmldb:exist:///db/ddi/xsl/ddi_3_1/ddi3_1.xsl"), $params)

let $fo := transform:transform($xhtml, xs:anyURI("xmldb:exist:///db/ddi/xsl/util/xhtml2fo.xsl"), $params)



let $pdf := xslfo:render($fo, "application/pdf", ())  
return response:stream-binary($pdf, "application/pdf", "output.pdf")

